package com.example.myapplication

import com.abc.robot01.activity.toJSONString
import com.abc.robot01.fragment.SystemMessageFragment
import com.abc.robot01.retrofit.RetrofitService
import com.abc.robot01.retrofit.UploadFileService
import com.abc.robot01.retrofit.bean.ARTICLE
import com.abc.robot01.retrofit.bean.DESIGN_PACKAGE
import com.abc.robot01.retrofit.bean.FetchDataRequest
import com.abc.robot01.retrofit.bean.VIDEO
import com.alibaba.fastjson.JSON
import com.google.gson.GsonBuilder
import kotlinx.coroutines.test.runTest
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import org.junit.After
import org.junit.Assert
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.io.IOException

class HomePageServiceTest {
    @Test
    fun addition_isCorrect() {
        assertEquals(4, 2 + 2)
    }

    private  lateinit var  mockWebServer: MockWebServer ;

    private lateinit var homePageService: RetrofitService

    @Before
    @Throws(IOException::class)
    fun setup() {
        // Create a MockWebServer instance
        mockWebServer = MockWebServer()
        mockWebServer.start()
        // Create Gson instance
        val gson = GsonBuilder()
            .setLenient()
            .create()
        // Create Retrofit instance with MockWebServer URL and Gson converter
        val retrofit = Retrofit.Builder()
            .baseUrl(mockWebServer.url("/")) // Use mock web server URL
            .addConverterFactory(GsonConverterFactory.create(gson))
            .build()
        // Create an instance of your Retrofit service interface
//        homePageService = retrofit.create(HomePageService::class.java)
        homePageService = RetrofitService.instance
    }

    @After
    @Throws(IOException::class)
    fun tearDown() {
        // Shutdown the MockWebServer
        mockWebServer.shutdown()
    }

    @Test
    fun testGetHomePage() = runTest {
     val result  = homePageService.getHomePage("95965a785a1611ef95b6ac1f6bb35aa0","11")
        System.out.println(result.toJSONString())
        assertEquals(200, result.code)
        assertEquals(3, result.data.size)
        result.data.forEach {
          if(it.configType== DESIGN_PACKAGE)
          {
          it.configs.forEach{ config ->
              System.out.println("DESIGN_PACKAGE renderUrl:"+ config.renderUrl)
          }
          }
            if(it.configType== VIDEO)
            {
                it.configs.forEach{ config ->
                    System.out.println("VIDEO fileUrl:"+ config.fileUrl)
                }
            }

            if(it.configType==ARTICLE)
            {
                it.configs.forEach{ config ->
                    System.out.println("ARTICLE fileUrl:"+ config.fileUrl)
                }
            }

        }
        assertEquals("DESIGN_PACKAGE", result.data[0].configType)
        assertEquals(ARTICLE, result.data[1].configType)
    }


    @Test
    fun testGetHomePageCommodity() = runTest {
        val result  = homePageService.getHomePageCommodity(FetchDataRequest(0,1,"95965a785a1611ef95b6ac1f6bb35aa0","219798adc03040a69c63ba3194874d0a"))
        System.out.println(result.toJSONString())
        result.data.list.forEach { item ->
            item.thumbnail.let {
                if(it?.trim().equals("null").not()){
                System.out.println(it)
                System.out.println(item.toJSONString())
            }}
        }
        assertEquals(200, result.code)
        assertEquals(1, result.data.list.size)





//        assertEquals(3, result.data.size)
//        result.data.forEach {
//            if(it.configType=="DESIGN_PACKAGE")
//            {
//                it.configs.forEach{ config ->
//                    System.out.println("DESIGN_PACKAGE renderUrl:"+ config.renderUrl)
//                }
//            }
//            if(it.configType=="VIDEO")
//            {
//                it.configs.forEach{ config ->
//                    System.out.println("VIDEO fileUrl:"+ config.fileUrl)
//                }
//            }
//
//            if(it.configType=="ARTICLE")
//            {
//                it.configs.forEach{ config ->
//                    System.out.println("ARTICLE fileUrl:"+ config.fileUrl)
//                }
//            }
//
//        }
//        assertEquals("DESIGN_PACKAGE", result.data[0].configType)
//        assertEquals("ARTICLE", result.data[1].configType)
    }



//    /**
//     *  检查机器状态
//     */
//    @Test
//    fun testLogin() = runTest {
//        val result  = homePageService.login("2674E5").string()
//        System.out.println(result)
//        if(result.contains("""
//            "userId":null
//        """.trimIndent())){
//            System.out.println("机器未激活")
//            val responseBean = JSON.parseObject(result, ResponseBean2::class.java)
//            assertEquals(responseBean.code, 200)
//            System.out.println(responseBean.data!!.qrCode)
//        }


//        result.data.list.forEach { item ->
//            item.thumbnail.let {
//                if(it?.trim().equals("null").not()){
//                    System.out.println(it)
//                    System.out.println(item.toJSONString())
//                }}
//        }
//        assertEquals(200, result.code)
//        assertEquals(200, 1)
//        assertEquals(1, result.data.list.size)
//    }


    @Test
    fun a1(){
        assertTrue(SystemMessageFragment.monthList[1].items[2].date == "2023年8月3日 15:45")
        SystemMessageFragment.delete(SystemMessageFragment.monthList[1].items[2]);
        assertTrue(SystemMessageFragment.monthList[1].items[2].date != "2023年8月3日 15:45")
        assertTrue(SystemMessageFragment.monthList[1].items[2].date == "2023年8月4日 13:10")
    }
}

